package com.qingmi.farmer.data;

import android.databinding.ObservableField;

import com.blankj.utilcode.util.StringUtils;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.repository.data.BasicDataRepository;
import com.wjjun.base.ui.base.BaseActivity;
import com.wjjun.utils.RegexUtils;

import javax.inject.Inject;

/**
 * 数据验证工具
 */


@SuppressWarnings("ConstantConditions")
public class DataVerifyUtils {

    private BaseActivity activity;

    private boolean result;

    @Inject
    BasicDataRepository basicDataRepository;

    public DataVerifyUtils(BaseActivity activity) {
        this.activity = activity;
        reset();

    }

    public DataVerifyUtils reset() {
        result = true;
        return this;
    }

    /**
     * 名字
     */
    public DataVerifyUtils verifyName(String name) {
        if (!result) {
            return this;
        }

        if (isEmpty(name)) {
            showToast(R.string.error_name_is_empty);
            result = false;
        }

        return this;
    }

    /**
     * 密码
     */
    public DataVerifyUtils verifyPwd(String pwd) {
        if (!result) {
            return this;
        } else if (isEmpty(pwd)) {
            showToast(R.string.error_pwd_is_empty);
            result = false;
        } else if (pwd.length() < 6 || pwd.length() > 20) {
            showToast(R.string.error_pwd_is_error);
            result = false;
        }
        return this;
    }

    /**
     * 密码
     */
    public DataVerifyUtils verifyPwd(String pwd, String pwdAgain) {
        if (!result) {
            return this;
        } else if (isEmpty(pwd)) {
            showToast(R.string.error_pwd_is_empty);
            result = false;
        } else if (isEmpty(pwdAgain)) {
            showToast(R.string.error_pwd_again_is_empty);
            result = false;
        } else if (pwd.length() < 6 || pwd.length() > 20) {
            showToast(R.string.error_pwd_is_error);
            result = false;
        } else if (!pwd.equals(pwdAgain)) {
            showToast(R.string.error_pwd_not_equal);
            result = false;
        } else if (!RegexUtils.isMixPwd(pwd)) {    //验证字母、数字和符号2种或以上组合，6-20字符
            showToast(R.string.error_pwd_is_error);
            result = false;
        }
        return this;
    }

    /**
     * 支付密码
     */
    public DataVerifyUtils verifyPayPwd(String pwd, String pwdAgain) {
        if (!result) {
            return this;
        } else if (isEmpty(pwd)) {
            showToast(R.string.error_new_pwd_is_empty);
            result = false;
        } else if (isEmpty(pwdAgain)) {
            showToast(R.string.error_pwd_again_is_empty);
            result = false;
        } else if (pwd.length() < 6 || pwd.length() > 20) {
            showToast(R.string.error_pwd_is_error);
            result = false;
        } else if (!pwd.equals(pwdAgain)) {
            showToast(R.string.error_pwd_not_equal);
            result = false;
        }
        return this;
    }

    /**
     * 手机号
     */
    public DataVerifyUtils verifyPhone(String phone) {
        if (!result) {
            return this;
        }

        if (isEmpty(phone)) {
            showToast(R.string.error_phone_is_empty);
            result = false;
        }
        return this;
    }

    /**
     * 验证码
     */
    public DataVerifyUtils verifyCode(String code) {
        if (!result) {
            return this;
        } else if (isEmpty(code)) {
            showToast(R.string.error_code_is_empty);
            result = false;
        } else if (code.length() != 6) {
            showToast(R.string.error_code_is_error);
            result = false;
        }
        return this;
    }

    /**
     * 头像
     */
    public DataVerifyUtils verifyHeadImg(String url) {
        if (isEmpty(url)) {
            showToast(R.string.error_head_img_is_empty);
            result = false;
        }
        return this;
    }

    /**
     * 身份证
     */
    public DataVerifyUtils verifyIdCard(String idCard) {
        if (!result) {
            return this;
        } else if (isEmpty(idCard)) {
            showToast(R.string.error_cid_is_empty);
            result = false;
        } else if (!RegexUtils.isIDCard(idCard)) {
            showToast(R.string.error_cid_is_error);
            result = false;
        }
        return this;
    }

    /**
     * 地址
     */
    public DataVerifyUtils verifyAddress(String cityId, String address) {
        if (!result) {
            return this;
        }

        if (!isEmpty(address) && isEmpty(cityId)) {
            showToast(R.string.error_city_id_is_empty);
            result = false;
        }

        return this;
    }

    /**
     * 土地位置
     */
    public DataVerifyUtils verifyLandLocation(String location) {
        if (!result) {
            return this;
        }

        if (isEmpty(location)) {
            showToast(R.string.error_supervise_is_empty);
            result = false;
        }

        return this;
    }

    /**
     * 水田面积
     */
    public DataVerifyUtils verifyWaterAcreage(ObservableField<Double> waterAcreage) {
        if (!result) {
            return this;
        }

        if (waterAcreage.get() == null || waterAcreage.get() < 0) {
            showToast(R.string.error_water_acreage_is_empty);
            result = false;
        }

        return this;
    }

    /**
     * 旱田面积
     */
    public DataVerifyUtils verifyDryFieldAcreage(ObservableField<Double> dryField) {
        if (!result) {
            return this;
        }

        if (dryField == null || dryField.get() == null || dryField.get() < 0) {
            showToast(R.string.error_dry_filed_acreage_is_empty);
            result = false;
        }

        return this;
    }

    /**
     * 总面积
     */
    public DataVerifyUtils verifyTotalAcreage(ObservableField<Double> totalAcreage) {
        if (!result) {
            return this;
        }

        if (totalAcreage.get() == null || totalAcreage.get() < 0) {
            showToast(R.string.error_total_acreage_is_empty);
            result = false;
        }
        return this;
    }

    /**
     * 合作社
     */
    public DataVerifyUtils verifyCooperative(String cooperativeId) {
        if (!result) {
            return this;
        }

        if (isEmpty(cooperativeId)) {
            showToast(R.string.error_cooperative_is_null);
            result = false;
        }
        return this;
    }

    public boolean startVerify() {
        return result;
    }

    private boolean isEmpty(String string) {
        return StringUtils.isTrimEmpty(string);
    }

    private void showToast(int resId) {
        if (activity != null) {
            activity.showToast(resId);
        }
    }

}
